﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RFID信息初始化工作台
{
    public partial class Form2 : Form
    {
        FK _fk = new FK();
        private int _com, _bl;
        private string _comName;

        Form1 _f1;

        public Form2(Form1 f1)
        {
            InitializeComponent();
            //_com = com;
            //_bl = bl;
            //_comName = comName;

            _f1 = f1;

           // _fk = new FK();
            //if (!_fk.Connection(com, bl, comName))
            //    return;
        }

        private void btn_Read_Click(object sender, EventArgs e)
        {
            listView_File2.Items.Clear();
            //if(!_fk.Connection(_com, _bl, _comName))
            //{
            //    ///MessageBox.Show("与设备通讯发生错误!");
            //    return;
            //}
            int count = 0;
            string s = _fk.ReadEPC(out count);
            ///_fk.Close();
            if (s == "")
                MessageBox.Show("未读到标签信息!");
            else if (!s.StartsWith("AAAA"))
                MessageBox.Show("标签格式不符合规范!");
            else
            {
                try
                {
                    //YDRFIDEntities conn = new YDRFIDEntities();
                    //var n = conn.T_RFID.Select(c => c).Where(c => c.RFID_TAG == s).Single();
                    //string tm = (n.RFID_TM == null ? "" : n.RFID_TM);
                    //string tag = (n.RFID_TAG == null ? "" : n.RFID_TAG);
                    //ListViewItem item = new ListViewItem(new string[] { "1", tag, n.RFID_ATYPE, n.RFID_DH, tm, n.RFID_DATE.Value.ToString("yyyy-MM-dd HH:mm"), n.RFID_USER });
                    //item.Tag = n;
                    //listView_File2.Items.Add(item);
                }
                catch
                {
                    MessageBox.Show("未找到匹配的档案信息!");
                }
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //if (!_fk.Connection())
            //{
            //    MessageBox.Show("未连接发卡设备，或者没有安装发卡设备的驱动!");
            //}
        }

        private void btn_Writer_Click(object sender, EventArgs e)
        {
            if (listView_File2.Items.Count != 1)
            {
                MessageBox.Show("请先读取标签信息!");
                return;
            }

            if (listView_File2.Items[0].SubItems[1].Text != "")
            {
                if (MessageBox.Show("此标签已经初始化过，还需要再次初始化吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                    return;
            }

            string tag = "AAAA" + Commons._enUser.U_ID.ToString().PadLeft(2, '0') + DateTime.Now.ToString("yyyyMMddhhmmssffff");
            
            //if(!_fk.Connection(_com, _bl, _comName))
            //{
            //    MessageBox.Show("连接设备失败!");
            //    return;
            //}
            if (!_fk.writeEPC(tag))
                MessageBox.Show("标签初始化失败!");
            else
            {
                
                //var n = listView_File2.Items[0].Tag as T_RFID;

                //YDRFIDEntities conn = new YDRFIDEntities();
                //var query = conn.T_RFID.Select(c => c).Where(c => c.RFID_ID == n.RFID_ID);
                //foreach (var nn in query)
                //{
                //    nn.RFID_DATE = DateTime.Now;
                //    nn.RFID_TAG = tag;
                //}
                //conn.SaveChanges();
                //MessageBox.Show("重写成功!");
                //listView_File2.Items.Clear();
            }

            ///_fk.Close();
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
           
            _fk.Close();
            _fk.EndClose();
        }

        private void btn_Del_Click(object sender, EventArgs e)
        {
            if (listView_File2.Items.Count != 1)
            {
                MessageBox.Show("请先读取标签信息!");
                return;
            }

            if (listView_File2.Items[0].SubItems[1].Text != "")
            {
                if (MessageBox.Show("此标签已经初始化过，确定要抹除信息吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                    return;
            }

            //var n = listView_File2.Items[0].Tag as T_RFID;

            //YDRFIDEntities conn = new YDRFIDEntities();
            //var query = conn.T_RFID.Select(c => c).Where(c => c.RFID_ID == n.RFID_ID);
            //foreach (var nn in query)
            //{
            //    nn.RFID_USER = null;
            //    nn.RFID_DATE = null;
            //    nn.RFID_TAG = null;
            //}
            //conn.SaveChanges();
            //MessageBox.Show("抹除成功!");
            //listView_File2.Items.Clear();
        }
    }
}
